//
//  DiyRefreshHeader.m
//  iShow
//
//  Created by zhaohong on 16/4/8.
//  Copyright © 2016年 zhaohong. All rights reserved.
//

#import "DiyRefreshHeader.h"

@interface DiyRefreshHeader()
@property (weak, nonatomic) UILabel *label;
@property (weak, nonatomic) UIImageView *logo;
@end

@implementation DiyRefreshHeader
#pragma mark - 重写方法
#pragma mark 在这里做一些初始化配置（比如添加子控件）
- (void)prepare
{
    [super prepare];
    
    
    
    // 添加label
    UILabel *label = [[UILabel alloc] init];
    label.textColor = [UIColor hexColor:@"959BAA"];
    label.font = [UIFont boldSystemFontOfSize:11];
    label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:label];
    self.label = label;
    
    
    
    // logo
    UIImageView *logo = [[UIImageView alloc]init];
    logo.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:logo];
    self.logo = logo;
    // 设置正在刷新状态的动画图片
    NSMutableArray *freshStateRefreshingImages = [NSMutableArray array];
    for (NSUInteger i = 1; i<=5; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"下拉刷新-动画第%ld帧", i]];
        [freshStateRefreshingImages addObject:image];
        
    }
    self.logo.animationImages = freshStateRefreshingImages;
    self.logo.animationDuration = 0.5;
    UIImage *image = [UIImage imageNamed:@"下拉刷新-向下拉动刷新"];
    self.logo.image = image;
    // 设置控件的高度
    self.mj_h = self.logo.image.size.height+20;
    
    
}

#pragma mark 在这里设置子控件的位置和尺寸
- (void)placeSubviews
{
    [super placeSubviews];
    
    self.logo.frame = CGRectMake((self.width-self.logo.image.size.width)/2.0, self.height-self.logo.image.size.height, self.logo.image.size.width, self.logo.image.size.height);
    
    
    self.label.frame = CGRectMake(0, self.logo.top-20, self.width, 15);
    
    
}

#pragma mark 监听scrollView的contentOffset改变
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
{
    [super scrollViewContentOffsetDidChange:change];
    
}

#pragma mark 监听scrollView的contentSize改变
- (void)scrollViewContentSizeDidChange:(NSDictionary *)change
{
    [super scrollViewContentSizeDidChange:change];
    
}

#pragma mark 监听scrollView的拖拽状态改变
- (void)scrollViewPanStateDidChange:(NSDictionary *)change
{
    [super scrollViewPanStateDidChange:change];
    
}

#pragma mark 监听控件的刷新状态
- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState;
    
    switch (state) {
        case MJRefreshStateIdle:
        {
            // 设置普通状态的动画图片
            
            [self.logo stopAnimating];
            UIImage *image = [UIImage imageNamed:@"下拉刷新-向下拉动刷新"];
            self.logo.image = image;
            
            
            self.label.text = @"下拉刷新数据";
        }
            
            
            
            break;
        case MJRefreshStatePulling:
        {
            // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
            
            UIImage *image2 = [UIImage imageNamed:@"下拉刷新-释放刷新"];
            self.logo.image = image2;
            [self.logo stopAnimating];
            self.label.text = @"放开即可刷新";
        }
            
            
            
            break;
        case MJRefreshStateRefreshing:
        {
            self.label.text = @"加载数据中";
            
            [self.logo startAnimating];
            
            
        }
            break;
            
        default:
            break;
    }
}

#pragma mark 监听拖拽比例（控件被拖出来的比例）
- (void)setPullingPercent:(CGFloat)pullingPercent
{
    [super setPullingPercent:pullingPercent];
    
    //    // 1.0 0.5 0.0
    //    // 0.5 0.0 0.5
    //    CGFloat red = 1.0 - pullingPercent * 0.5;
    //    CGFloat green = 0.5 - 0.5 * pullingPercent;
    //    CGFloat blue = 0.5 * pullingPercent;
    //    self.label.textColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}

@end

